﻿using UnityEngine;

public enum MapProjectionType
{
    等差分纬线多圆锥,
    伪墨卡托投影
}

public static class UvMapTool
{

    #region 伪墨卡托投影
    const float max_Value = Mathf.PI;
    const float min_Value = -Mathf.PI;

    static readonly float maxLat = Mathf.Atan(Mathf.Exp(max_Value)) * 2 - Mathf.PI / 2;
    static readonly float minLat = Mathf.Atan(Mathf.Exp(min_Value)) * 2 - Mathf.PI / 2;

    public static Vector2 PseudoMercatorProjection(Vector2 origin)
    {
        return new Vector2(
            origin.x, 
            PseudoMercatorProjection(origin.y));
    }

    public static float PseudoMercatorProjection(float lat)
    {
        if (lat > maxLat)
            return 1;
        else if (lat < minLat)
            return 0;
        else
        {
            var lat2yResult = Mathf.Log(Mathf.Tan(lat / 2 + Mathf.PI / 4));
            return (lat2yResult - min_Value) / (max_Value - min_Value);
        };
    }
    #endregion

    public static Vector2 CustomProjection(Vector2 origin, CustomUvMapper mapper)
    {
        return mapper.ToTarget(origin);
    }
}
